package com.sebiotec.manager.ui.fragment;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelProviders;

import com.allen.library.RxHttpUtils;
import com.sebiotec.basecommon.utils.base.BaseLazyLoadingFragment;
import com.sebiotec.manager.R;
import com.sebiotec.manager.R2;
import com.sebiotec.manager.model.event.RefEvent;
import com.sebiotec.manager.model.response.DashboardResponse;
import com.sebiotec.manager.ui.activity.QuickAddActivity;
import com.sebiotec.manager.viewmodel.BookViewModel;
import com.jakewharton.rxbinding2.view.RxView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;

public class BookFragment extends BaseLazyLoadingFragment {

    @BindView(R2.id.tv_upcoming_nb1)
    TextView tvUpcomingNb1;
    @BindView(R2.id.tv_pending_nb)
    TextView tvPendingNb;
    @BindView(R2.id.tv_reject_nb)
    TextView tvRejectNb;
    @BindView(R2.id.tv_upcoming)
    TextView tvUpcoming;
    @BindView(R2.id.tv_pending)
    TextView tvPending;
    @BindView(R2.id.tv_reject)
    TextView tvReject;
    @BindView(R2.id.fl_content_book)
    FrameLayout flContentBook;
    @BindView(R2.id.tv_add_book)
    TextView tvAddBook;
    private UpComingFragment upComingFragment;
    private Fragment[] fragments;
    private int lastfragment = 0;
    private RejectedFragment rejectedFragment;
    private PendingFragment pendingFragment;
    private BookViewModel mBookViewModel;
    private String Date;
    private String Filter;
    private int IsBookmark;


    @Override
    protected String getLogTag() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.man_fragment_book;
    }

    @Override
    protected void initView(View root) {
        EventBus.getDefault().register(this);
        mBookViewModel = ViewModelProviders.of(this).get(BookViewModel.class);
//        aCache = ACache.get(getActivity(),ETFConstant.APPHOMEPATH);


        TextView viewById = root.findViewById(R.id.tv_add_book);
        RxView.clicks(viewById)
                .throttleFirst(2, TimeUnit.SECONDS)
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        startActivity(QuickAddActivity.class);
                    }
                });
        InitUI();
    }

    @Override
    protected void onFragmentResume() {
        super.onFragmentResume();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        RxHttpUtils.cancel("ManagerBook");
    }

    private void InitUI() {



        upComingFragment = new UpComingFragment();
        pendingFragment = new PendingFragment();
        rejectedFragment = new RejectedFragment();

        fragments = new Fragment[]{upComingFragment, pendingFragment, rejectedFragment};

        //设置fragment到布局
        getChildFragmentManager().beginTransaction().replace(R.id.fl_content_book, upComingFragment).show(upComingFragment).commit();
    }


    @OnClick({R2.id.tv_upcoming, R2.id.tv_pending, R2.id.tv_reject})
    public void onViewClicked(View view) {
        int white = getActivity().getResources().getColor(R.color.white);
        int green = getActivity().getResources().getColor(R.color.smj);
        int id = view.getId();
        if (id == R.id.tv_upcoming) {
            if (lastfragment != 0) {
                switchFragment(lastfragment, 0);
                lastfragment = 0;
            }
            tvUpcoming.setEnabled(false);

            tvUpcoming.setTextColor(green);
            tvPending.setEnabled(true);
            tvPending.setTextColor(white);
            tvReject.setEnabled(true);
            tvReject.setTextColor(white);
        } else if (id == R.id.tv_pending) {
            if (lastfragment != 1) {
                switchFragment(lastfragment, 1);
                lastfragment = 1;
            }
            tvUpcoming.setEnabled(true);
            tvUpcoming.setTextColor(white);
            tvPending.setEnabled(false);
            tvPending.setTextColor(green);
            tvReject.setEnabled(true);
            tvReject.setTextColor(white);
        } else if (id == R.id.tv_reject) {
            if (lastfragment != 2) {
                switchFragment(lastfragment, 2);
                lastfragment = 2;
            }
            tvUpcoming.setEnabled(true);
            tvUpcoming.setTextColor(white);
            tvPending.setEnabled(true);
            tvPending.setTextColor(white);
            tvReject.setEnabled(false);
            tvReject.setTextColor(green);
        }
    }


    /**
     * 切换fragment
     */
    private void switchFragment(int lastfragment, int index) {
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        //隐藏上个Fragment
        transaction.hide(fragments[lastfragment]);
        if (fragments[index].isAdded() == false) {
            transaction.add(R.id.fl_content_book, fragments[index]);
        }
        transaction.show(fragments[index]).commit();
    }


    public void initData() {
        mBookViewModel.getData(Date,Filter,IsBookmark,new BookViewModel.MyBookDataCallBack<DashboardResponse>() {
            @Override
            public void onDataSuccessCallBack(DashboardResponse dashboardResponse) {
                tvUpcomingNb1.setText(dashboardResponse.getUpcoming());
                tvPendingNb.setText(dashboardResponse.getPending());
                tvRejectNb.setText(dashboardResponse.getRejected());
            }

            @Override
            public void onDataFailCallBack(DashboardResponse dashboardResponse) {

            }
        });
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void retf(RefEvent event){
        Date=event.getDate();
        Filter=event.getFilter();
        IsBookmark=event.getIsBookmark();

        initData();
    }

}
